<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">

<div id="app">

    <div class="content-header">
        <h1>系统设置
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统设置</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="用户名" style="width: 200px;" class="filter-item"
                          v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                                                                            <!--弹出添加页面-->
                <el-button type="primary" class="butT" @click="handleCreate()">新建用户</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="list" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="username" label="用户名" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>
                <el-table-column prop="birthday" label="出生日期" align="center"></el-table-column>
                <el-table-column prop="telephone" label="电话号码" align="center"></el-table-column>
                <el-table-column prop="station" label="状态" align="center"></el-table-column>
                <el-table-column prop="remark" label="备注" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">

                                <!--弹出编辑和删除表单-->

                        <el-button type="primary" size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-pagination
                    class="pagination"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
            </el-pagination>

            <div class="add-form">
                <el-dialog title="新建用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form ref="dataAddForm" :model="user" label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="user.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password">
                                                <el-input type="password" v-model="user.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-radio-group v-model="user.gender" prop="gender" size="small">
                                                    <el-radio label="男" value="男">男</el-radio>
                                                    <el-radio label="女" value="女">女</el-radio>
                                                </el-radio-group>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="user.birthday"
                                                                type="date"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="手机号">
                                                <el-input v-model="user.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="user.station">
                                                    <el-option label="开启" value="0"></el-option>
                                                    <el-option label="关闭" value="1"></el-option>
                                                </el-select>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="user.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键词</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>

                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible = false">取消</el-button>
                            <el-button type="primary" @click="handleAdd()">确定</el-button>
                        </div>
                    </template>
                </el-dialog>

            </div>

            <div class="add-form">
                <el-dialog title="编辑用户" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">

                                <el-form ref="dataAddForm" :model="user" label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户名" prop="username">
                                                <el-input v-model="user.username"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="密码" prop="password">
                                                <el-input type="password" v-model="user.password"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-radio-group v-model="user.gender" prop="gender" size="small">
                                                    <el-radio label="男" value="男">男</el-radio>
                                                    <el-radio label="女" value="女">女</el-radio>
                                                </el-radio-group>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="user.birthday"
                                                                type="date"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="手机号">
                                                <el-input v-model="user.telephone"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="状态">
                                                <el-select v-model="user.station">
                                                    <el-option label="开启" value="0"></el-option>
                                                    <el-option label="关闭" value="1"></el-option>
                                                </el-select>

                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="备注">
                                                <el-input v-model="user.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>关键词</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roleIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>

                        <div slot="footer" class="dialog-footer">
                            <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                                                       <!--编辑功能-->
                            <el-button type="primary" @click="handleUpdate()">确定</el-button>
                        </div>
                    </template>
                </el-dialog>
            </div>
        </div>
    </div>
</div>

</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            //用户表基本数据
            user: {},
            //整个页面的分页数据
            list: [],
            //添加窗口
            dialogFormVisible: false,
            //编辑窗口
            dialogFormVisible4Edit: false,
            //角色数据列表
            roleList: [],
            //复选框
            roleIds: [],
            //新增和编辑表单中角色对应的列表数据
            tableData: [],
            activeName: "first"
        },
        mounted() {
            this.findPage();
        },
        methods: {
            //弹出编辑按钮
            handleEdit(row) {
                this.dialogFormVisible4Edit = true;
                this.activeName = "first";
                //查询该用户的基本信息
                var url = "/user/findById?id="+row.id;
                axios.get(url).then(response=>{
                    if(response.data.flag){
                        this.user = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                });

                //查询所有角色的信息
                var url = "/role/findAll";
                axios.get(url).then((response)=>{
                    if(response.data.flag){
                        this.tableData = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                });

                //查询跟该用户关联的角色信息
                var url = "/user/"+row.id+"/roleIds";
                axios.get(url).then((response)=>{
                    if(response.data.flag){
                        this.roleIds = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                });

            },
            //编辑功能
            handleUpdate() {
                this.dialogFormVisible4Edit = false;
                var url = "/user/update?roleIds="+ this.roleIds;
                axios.put(url,this.user).then((response)=>{
                    if(response.data.flag){
                        this.$message.success(response.data.message);
                        return;
                    }
                    this.$message.error(response.data.message);
                }).finally(()=>{
                    this.findPage();
                })
            },
            //弹出新建按钮
            handleCreate() {
                this.dialogFormVisible = true;
                this.activeName = "first";
                this.user = {};
                this.roleIds = [];
                var url = "/role/findAll";
                axios.get(url).then((response)=>{
                    if(response.data.flag){
                        this.tableData = response.data.data;
                        return;
                    }
                    this.$message.error(response.data.message);
                })
            },
            //添加功能
            handleAdd() {
                this.$refs['dataAddForm'].validate((valid)=>{
                    if(valid){
                       var url = "/user/add?roleIds=" + this.roleIds;
                       axios.post(url,this.user).then((response)=>{
                           if(response.data.flag){
                               this.dialogFormVisible = false;
                               this.$message.success(response.data.message);
                               return;
                           }
                           this.$message.error(response.data.message);
                       }).finally(()=>{
                           this.findPage();
                       })
                    }
                })
            },
            //删除功能
            handleDelete(row) {
                var url = "/user/delete?userId="+row.id;
                this.$confirm("您确定要删除此项嘛?","提示",{
                    confirmButtonText:'确定',
                    cancelButtonText:'取消',
                    type:'warning'
                }).then(()=>{
                    //发送异步请求
                    axios.delete(url).then((response)=>{
                        if(response.data.flag){
                            this.$message.success(response.data.message);
                            return;
                        }
                        this.$message.error(response.data.message);
                    }).finally(()=>{
                        this.findPage();
                    })
                })

            },
            //分页查询
            findPage() {
                var params = {
                    currentPage : this.pagination.currentPage,
                    pageSize :this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                var url = "/user/findPage";
                //发送异步请求
                axios.get(url,{params:params}).then((response)=>{
                    if(response.data.flag){
                        this.list = response.data.data.rows;//列表数据列表
                        this.pagination.total = response.data.data.total;//数据总条数
                        return;
                    }
                    this.$message.error(response.data.message);
                })
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            }
        }
    })
</script>
</html>


